home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib25 / bsearch.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  1KB  |  51 lines

  1. /* from Dale Schumacher's dLibs library */
  2.  
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6.  
  7. /*
  8.  * This routine is safe in the sense that it does not make
  9.  * assumptions about sizeof(void *). Gcc assumes same as char *
  10.  * when not -ansi, the "other" compiler just barfs.
  11.  *
  12.  */
  13.  
  14. void * bsearch(key, base, num, size, cmp)
  15.     register const void * key;        /* item to search for */
  16.     register const void * base;        /* base address */
  17.     size_t          num;        /* number of elements */
  18.     register size_t   size;        /* element size in bytes */
  19.     /* comparison function */
  20.     register int (*cmp) __PROTO((const void *, const void *));
  21.     {
  22.     register size_t a, b, c;
  23.     register int dir;
  24.  
  25.     assert ((key != NULL) && (base != NULL) && (size > 0) && (num > 0) &&
  26.         (cmp != NULL));
  27.  
  28.     a = 0;
  29.     b = num - 1;
  30.     while(a <= b)
  31.         {
  32.         c = (a + b) >> 1;    /* == ((a + b) / 2) */
  33.         if ((dir = (*cmp)((void *)((char *)base + (c * size)), key)) != 0)
  34.             {
  35.             if (dir > 0)
  36.             {
  37.                 if (c == 0)
  38.                 return(NULL);
  39.                 b = c - 1;
  40.             }
  41.             else /* (dir < 0) */
  42.                 a = c + 1;
  43.             }
  44.         else
  45.             {
  46.             return((void *)(((char *)base) + (c * size)));
  47.             }
  48.         }
  49.     return(NULL);
  50.     }
  51.